package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.ChargeNorm;
import com.lrkj.payfee.service.IMeterBoxService;
import com.lrkj.property.domain.Community;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.MeterKindMapper;
import com.lrkj.payfee.domain.MeterKind;
import com.lrkj.payfee.service.IMeterKindService;

/**
 * 仪种类Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class MeterKindServiceImpl implements IMeterKindService
{
    @Autowired
    private MeterKindMapper meterKindMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询仪种类
     *
     * @param kindId 仪种类ID
     * @return 仪种类
     */
    @Override
    public MeterKind selectMeterKindById(String kindId)
    {
        return meterKindMapper.selectMeterKindById(kindId);
    }

    /**
     * 查询仪种类列表
     *
     * @param meterKind 仪种类
     * @return 仪种类
     */
    @Override
    public List<MeterKind> selectMeterKindList(MeterKind meterKind)
    {
        return meterKindMapper.selectMeterKindList(meterKind);
    }

    /**
     * 新增仪种类
     *
     * @param meterKind 仪种类
     * @return 结果
     */
    @Override
    public int insertMeterKind(MeterKind meterKind)
    {
        int row=0;
        try {
            meterKind.setKindId(meterKind.getKindCode());
            meterKind.setCreateBy(SecurityUtils.getUsername());
            meterKind.setCreateTime(DateUtils.getNowDate());
            row=meterKindMapper.insertMeterKind(meterKind);
        }catch (Exception e){
            if(e.getMessage().indexOf("for key 'PRIMARY'") != -1){
                throw new BaseException("代码已存在！");
            }
        }
//        this.setUnitInfo(meterKind);
        return row;
    }

    /**
     * 修改仪种类
     *
     * @param meterKind 仪种类
     * @return 结果
     */
    @Override
    public int updateMeterKind(MeterKind meterKind)
    {
//        this.setUnitInfo(meterKind);
        meterKind.setUpdateBy(SecurityUtils.getUsername());
        meterKind.setUpdateTime(DateUtils.getNowDate());
        return meterKindMapper.updateMeterKind(meterKind);
    }

    /**
     * 批量删除仪种类
     *
     * @param kindIds 需要删除的仪种类ID
     * @return 结果
     */
    @Override
    public int deleteMeterKindByIds(String[] kindIds)
    {
        orgTreeService.deleteOrgTreeByIds(kindIds);
        return meterKindMapper.deleteMeterKindByIds(kindIds);
    }

    /**
     * 删除仪种类信息
     *
     * @param kindId 仪种类ID
     * @return 结果
     */
    @Override
    public int deleteMeterKindById(String kindId)
    {
        orgTreeService.deleteOrgTreeById(kindId);
        return meterKindMapper.deleteMeterKindById(kindId);
    }


    private void setUnitInfo(MeterKind bean) {
        OrgTree parentBean = orgTreeService.selectOrgTreeById(bean.getDeptId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getDeptId());
        orgTreeService.insertOrgTree(bean.getKindId(), bean.getKindName(), bean.getDeptId(), OrgConstants.kind, bean.getAncestors());
    }
}
